// MainFrm.cpp : implementation of the CMainFrame class
//

#include "stdafx.h"
#include "TestAnaly.h"

#include "MainFrm.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

DD_ImgAnaly g_ImgAnaly;

/////////////////////////////////////////////////////////////////////////////
// CMainFrame

IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)

BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
	//{{AFX_MSG_MAP(CMainFrame)
	ON_WM_CREATE()
	ON_WM_SIZE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

static UINT indicators[] =
{
	ID_SEPARATOR,           // status line indicator
	ID_INDICATOR_CAPS,
	ID_INDICATOR_NUM,
	ID_INDICATOR_SCRL,
};

/////////////////////////////////////////////////////////////////////////////
// CMainFrame construction/destruction

CMainFrame::CMainFrame()
{
	// TODO: add member initialization code here
	m_bInit = FALSE;
	
	m_pToolView = NULL;
	m_pViewRef  = NULL;
	m_pViewSap  = NULL;
	m_pViewAdj  = NULL;
	m_pViewSub  = NULL;

	m_nImageViewMode = eShowRef;
}

CMainFrame::~CMainFrame()
{
}

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
	if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
		return -1;

	ShowWindow(SW_SHOWMAXIMIZED);

	if (!m_wndToolBar.CreateEx(this, TBSTYLE_FLAT, WS_CHILD | WS_VISIBLE | CBRS_TOP
		| CBRS_GRIPPER | CBRS_TOOLTIPS | CBRS_FLYBY | CBRS_SIZE_DYNAMIC) ||
		!m_wndToolBar.LoadToolBar(IDR_MAINFRAME))
	{
		TRACE0("Failed to create toolbar\n");
		return -1;      // fail to create
	}

	if (!m_wndStatusBar.Create(this) ||
		!m_wndStatusBar.SetIndicators(indicators,
		  sizeof(indicators)/sizeof(UINT)))
	{
		TRACE0("Failed to create status bar\n");
		return -1;      // fail to create
	}

	// TODO: Delete these three lines if you don't want the toolbar to
	//  be dockable
	//m_wndToolBar.EnableDocking(CBRS_ALIGN_ANY);
	//EnableDocking(CBRS_ALIGN_ANY);
	//DockControlBar(&m_wndToolBar);

	return 0;
}



BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT /*lpcs*/,
								CCreateContext* pContext)
{
	CRect rect;
	GetClientRect(&rect);

	// create splitter window
	if (!m_wndSplitter.CreateStatic(this, 1, 2))
		return FALSE;


	if ( !m_wndSplitter2.CreateStatic(&m_wndSplitter,2, 2, 	
		WS_CHILD | WS_VISIBLE,
		m_wndSplitter.IdFromRowCol( 0, 0 ) ) )
		return FALSE;


	int nWidth = rect.Width()-TOOLVIEWWIDTH;


	if( !m_RulersSap.CreateRulers( &m_wndSplitter2,m_wndSplitter2.IdFromRowCol( 0, 0 ),RUNTIME_CLASS(ViewSap),pContext) ) {return FALSE;}
	if( !m_RulersRef.CreateRulers( &m_wndSplitter2,m_wndSplitter2.IdFromRowCol( 0, 1 ),RUNTIME_CLASS(ViewRef),pContext) ) {return FALSE;}
	if( !m_RulersAdj.CreateRulers( &m_wndSplitter2,m_wndSplitter2.IdFromRowCol( 1, 0 ),RUNTIME_CLASS(ViewAdj),pContext) ) {return FALSE;}
	if( !m_RulersSub.CreateRulers( &m_wndSplitter2,m_wndSplitter2.IdFromRowCol( 1, 1 ),RUNTIME_CLASS(ViewSub),pContext) ) {return FALSE;}
	

	if( !m_wndSplitter.CreateView(0, 1, RUNTIME_CLASS(CToolView), CSize(TOOLVIEWWIDTH, 100), pContext ) )
	{
		m_wndSplitter.DestroyWindow();
		return FALSE;
	}
		
	m_pViewRef = (ViewRef *)m_RulersRef.GetView(); 
	m_pViewSap = (ViewSap *)m_RulersSap.GetView();
	m_pViewAdj = (ViewAdj *)m_RulersAdj.GetView();
	m_pViewSub = (ViewSub *)m_RulersSub.GetView();


	m_RulersRef.ShowRulers();
	m_RulersSap.ShowRulers();
	m_RulersAdj.ShowRulers();
	m_RulersSub.ShowRulers();


	m_pToolView  = (CToolView*)m_wndSplitter.GetPane(0,1);

	m_pToolView->m_pMainFrame     = this;
	m_pViewRef->m_pMainFrame      = this;
	m_pViewSap->m_pMainFrame      = this;
	m_pViewAdj->m_pMainFrame      = this;
	m_pViewSub->m_pMainFrame      = this;

	m_pViewRef->m_pRuler = &m_RulersRef;
	m_pViewSap->m_pRuler = &m_RulersSap;
	m_pViewAdj->m_pRuler = &m_RulersAdj;
	m_pViewSub->m_pRuler = &m_RulersSub;


	m_pToolView->Initialize();

	m_pViewRef->Initialize();
	m_pViewSap->Initialize();
	m_pViewAdj->Initialize();
	m_pViewSub->Initialize();

	m_pViewRef->m_bInit = true;
	m_pViewSap->m_bInit = true;
	m_pViewAdj->m_bInit = true;
	m_pViewSub->m_bInit = true;

	m_bInit = TRUE;
	
	return TRUE;
}



void CMainFrame::ShowRulers(BOOL bShow)
{
	m_RulersRef.ShowRulers(bShow);
	m_RulersSap.ShowRulers(bShow);
	m_RulersAdj.ShowRulers(bShow);
	m_RulersSub.ShowRulers(bShow);
}

void CMainFrame::UpdateRulersInfo(stRULER_INFO *pstDyRuler,stRULER_INFO *pstStRuler)
{
	if( (m_nImageViewMode & eShowRef) && NULL != pstDyRuler )
	{
		m_RulersRef.UpdateRulersInfo(*pstDyRuler);
	}
	if( (m_nImageViewMode & eShowSap) && NULL != pstStRuler )
	{
		m_RulersRef.UpdateRulersInfo(*pstDyRuler);
		m_RulersSap.UpdateRulersInfo(*pstDyRuler);
		m_RulersAdj.UpdateRulersInfo(*pstDyRuler);
		m_RulersSub.UpdateRulersInfo(*pstDyRuler);
	}
}

void CMainFrame::InvalidateRulers()
{
	if( m_nImageViewMode & eShowRef )
	{
		m_RulersRef.Invalidate();
	}
	if( m_nImageViewMode & eShowSap )
	{
		m_RulersRef.Invalidate();
		m_RulersSap.Invalidate();
		m_RulersAdj.Invalidate();
		m_RulersSub.Invalidate();
	}
}


BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
	if( !CFrameWnd::PreCreateWindow(cs) )
		return FALSE;
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return TRUE;
}

/////////////////////////////////////////////////////////////////////////////
// CMainFrame diagnostics

#ifdef _DEBUG
void CMainFrame::AssertValid() const
{
	CFrameWnd::AssertValid();
}

void CMainFrame::Dump(CDumpContext& dc) const
{
	CFrameWnd::Dump(dc);
}

#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CMainFrame message handlers


void CMainFrame::OnSize(UINT nType, int cx, int cy) 
{
	CFrameWnd::OnSize(nType, cx, cy);

	if( !m_bInit )
	{
		return;
	}
	
	// TODO: Add your message handler code here

	if( cx > TOOLVIEWWIDTH )
	{
		int nWidth = cx-TOOLVIEWWIDTH;
	
		m_wndSplitter.SetColumnInfo(0,nWidth,0);
		m_wndSplitter.SetColumnInfo(1,TOOLVIEWWIDTH,0);

		ShowPanelView();
	}
}


void CMainFrame::ShowPanelView()
{
	int nWidth,nHeight;
	CRect rt;
	m_wndSplitter2.GetClientRect(&rt);
	nWidth  = rt.Width()-10;
	nHeight = rt.Height()-10;

	int nW1 = 0;
	int nW2 = 0;
	int nH1 = 0;
	int nH2 = 0;

	if( m_nImageViewMode == eShowRef )
	{
		nW1 = 0;
		nW2 = nWidth+10;

		nH1 = nHeight+10;
		nH2 = 0;
	}
	else if( m_nImageViewMode == eShowSap )
	{
		nW1 = nWidth/2;
		nW2 = nWidth-nW1;

		nH1 = nHeight/2;
		nH2 = nHeight - nH1;
	}
	m_wndSplitter2.SetColumnInfo(0,nW1,0);
	m_wndSplitter2.SetColumnInfo(1,nW2,0);
	m_wndSplitter2.SetRowInfo(0,nH1,0);
	m_wndSplitter2.SetRowInfo(1,nH2,0);

	m_wndSplitter2.RecalcLayout();
}

void CMainFrame::SetShowMode(int nMode)
{
	m_nImageViewMode = nMode;
	ShowPanelView();
}